<?php
use Doctrine\ORM\NoResultException;
class Utilisateurs extends BaseCtrl{
	
	function __construct()
	{
		parent::__construct();
		$this->load->library('jsUtils');
	}
	
    public function index(){
    	$this->refresh();
    }
    
    public function refresh(){
    	$this->jsutils->postFormAndBindTo("#btConnect","click","/wikiPro/utilisateurs/connect","frmCo","body");
    	$this->jsutils->compile();
    	$this->load->view('v_header');
    	$this->load->view('v_connexion');
    	
    }
    
    private function _loadUtils(){
        $query = $this->doctrine->em->createQuery("SELECT u FROM Utilisateur u join u.groupe c");
        $users = $query->getResult();
        return $users;
    }

    public function testConnexion(){

    }

    public function connect(){

    	$postLogin=$_POST['login'];
    	$postPassword=$_POST['password'];

    	$query = $this->doctrine->em->createQuery("SELECT u FROM Utilisateur u WHERE u.login='".$postLogin."' AND u.password='".$postPassword."'");

    	try {
    		$user=$query->getSingleResult();
    	} catch (NoResultException  $e) {
    		echo "Login ou mot de passe invalide";
    		$this->load->view('connexion');
    	}

    	if (isset($user)){
    		
    		$this->session->set_userdata('user',$user);
    		$this->load->view('v_success_connect');
    		$aUser= $this->session->userdata('user');
    		var_dump($aUser);
    		
    		
    	}
   		
    	

    }	

}
?>